package com.example.demo.leetcode.level2;

import java.util.ArrayList;
import java.util.List;

public class 螺旋矩阵 {


    public static List<Integer> spiralOrder(int[][] matrix) {

        int count = matrix.length * matrix[0].length;

        int maxY = matrix[0].length;
        int maxX = matrix.length;

        int x=0,y=0;
        List<Integer> list = new ArrayList<>();

        int minY=0;
        int minX=1;

        while(list.size() < count){

            while(y < maxY && list.size() < count){
                list.add(matrix[x][y]);
                y++;
            }
            maxY--;
            y--;
            x++;

            while(x < maxX && list.size() < count){
                list.add(matrix[x][y]);
                x++;
            }
            maxX--;
            x--;
            y--;

            while(y >=minY && list.size() < count){
                list.add(matrix[x][y]);
                y--;
            }
            minY++;
            y++;
            x--;

            while(x >= minX && list.size() < count){
                list.add(matrix[x][y]);
                x--;
            }
            minX++;
            x++;
            y++;

        }

        return list;

    }

    public static void main(String[] args) {
        int [][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        List<Integer> integers = spiralOrder(matrix);
        integers.stream().forEach(x-> System.out.print(x+" "));
    }


}
